﻿#include "usermanagestandarditemmodel.h"
#include "tools.h"

UserManageStandardItemModel::UserManageStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{

}

void UserManageStandardItemModel::setHeader(UserRelation::RelationType type)
{
    QString nickName;
    QString nickNamePinyin;

    header.clear();

    if(type == UserRelation::Client){
        nickName = "客户名称";
        nickNamePinyin = "客户名称拼音";
    }else{
        nickName = "供应商名称";
        nickNamePinyin = "供应商名称拼音";
    }

    header<<nickName
         <<nickNamePinyin
        <<"联系人"
       <<"联系人拼音"
      <<"电话"
     <<"平台用户"
    <<"应开发票"
    <<"应收货款"
    <<"应收发票"
    <<"应付货款"
    <<"预开发票"
    <<"预收货款"
    <<"预收发票"
    <<"预付货款"
    <<"是否开票"
    <<"信用额度"
    <<"备注"
    <<"一级类别"
    <<"二级类别";

    setHorizontalHeaderLabels(header);
}

void UserManageStandardItemModel::reset(){
    clear();
    relations.clear();
}

void UserManageStandardItemModel::insertUserRelation(UserRelation &relation)
{
    QList<QStandardItem*> rowData;

    rowData.append(new QStandardItem(relation.nickName));
    rowData.append(new QStandardItem(relation.nickNamePinyin));
    rowData.append(new QStandardItem(relation.contact));
    rowData.append(new QStandardItem(relation.contactPinyin));
    rowData.append(new QStandardItem(relation.tel));
    rowData.append(new QStandardItem(relation.isPlatform?"是":"否"));
    rowData.append(new QStandardItem(QString::number(relation.ticketOut, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.moneyIn, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.ticketIn, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.moneyOut, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.preTicketOut, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.preMoneyIn, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.preTicketIn, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(relation.preMoneyOut, 'f', 2)));
    rowData.append(new QStandardItem(relation.hasTicketHistory?"不开票":"开票"));
    rowData.append(new QStandardItem(QString("%1").arg(relation.credit)));
    rowData.append(new QStandardItem(relation.remark));
    rowData.append(new QStandardItem(relation.primaryDirectory));
    rowData.append(new QStandardItem(relation.secondaryDirectory));

    appendRow(rowData);
    relations.append(&relation);
}

void UserManageStandardItemModel::insertUserRelation(QList<UserRelation*> relations, bool isShowPlatformUser){
    for(UserRelation *relation:relations){
        if(relation->isPlatform && isShowPlatformUser == false){
            //平台用户，且选择不显示
            continue;
        }
        insertUserRelation(*relation);
    }
}

UserRelation* UserManageStandardItemModel::getUserRelation(int index){
    if(index<0 || index>=relations.size()){
        return nullptr;
    }

    return relations[index];
}
